<?php

namespace app\admin\service;

use app\model\MountedAppletCustomRechargeConfig;
use app\model\RechargeTemplate;
use app\model\ThirdVideos;
use think\facade\Db;
use app\model\CustomRechargeTemplate;
use app\common\Error;

class VideoDetailService
{
    private $mountedAppletCustomRechargeConfigModel;
    private $rechargeTemplateModel;
    private $customRechargeTemplateModel;

    public function __construct(MountedAppletCustomRechargeConfig $mountedAppletCustomRechargeConfigModel, RechargeTemplate $rechargeTemplateModel, CustomRechargeTemplate $customRechargeTemplateModel)
    {
        $this->mountedAppletCustomRechargeConfigModel = $mountedAppletCustomRechargeConfigModel;
        $this->rechargeTemplateModel = $rechargeTemplateModel;
        $this->customRechargeTemplateModel = $customRechargeTemplateModel;
    }

    // 添加/编辑挂载的小程序自定义收费配置
    public function editMountedRechargeConfig($params)
    {
        if (empty($params['id'])) {
            $params['created_at'] = time();
            $params['is_new_version'] = 1;
        } else {
            $params['updated_at'] = time();
            $params['is_new_version'] = 1;
        }

        if (empty($params['id'])) {
            $info = $this->mountedAppletCustomRechargeConfigModel->getInfoByWhere(['platform' => $params['platform'], 'video_id' => $params['video_id']], 'recharge_template_id');
            if ($info) {
                return ['message' => Error::THE_VIDEO_SETTING_PLATFORM_MOUNTED_CONFIG];
            }
        }

        $bool = $this->mountedAppletCustomRechargeConfigModel->edit($params);
        // if ($info) {
            // 每个平台统一用一套模板
            // if ($params['recharge_template_id'] != $info['recharge_template_id']) {
            //     Db::name('mounted_applet_custom_recharge_config')->where(['platform' => $params['platform']])->update(['recharge_template_id' => $params['recharge_template_id'], 'is_new_version' => 1]);
            // }
        // }
        return $bool;
    }

    // 根据短剧ID获取配置的自定义收费配置
    public function getMountedRechargeConfigByVideoId($videoId,$detailId)
    {
        $data = $this->mountedAppletCustomRechargeConfigModel->getDataByWhere(['video_id' => $videoId], 'id, platform, sort, pay_amount, recharge_template_id, is_new_version');
        if (!empty($data)) {
            // $detailId = Db::name('video_details')->where('video_id', $videoId)->order('sort','asc')->value('id');
            list($dy_videos, $dy_details) = ThirdVideos::getDyMediaParam([$videoId], [$detailId]);
            foreach ($data as $key => $value) {
                if (empty($value['is_new_version'])) {
                    $rechargeTemplateData = $this->rechargeTemplateModel->getInfoByWhere(['id' => $value['recharge_template_id']], 'name');
                } else {
                    $rechargeTemplateData = $this->customRechargeTemplateModel->getInfoByWhere(['id' => $value['recharge_template_id']], 'name');
                }

                $data[$key]['recharge_template_name'] = !empty($rechargeTemplateData['name']) ? $rechargeTemplateData['name'] : '';
                $data[$key]['dy_param'] = '';
                if ($value['platform'] == 1) {
                    $album_id = $dy_videos[$videoId] ?? '';
                    $episode_id = $dy_details[$detailId] ?? '';
                    if ($album_id && $episode_id) {
                        $data[$key]['dy_param'] = '&tt_album_id=' . $album_id . '&tt_episode_id=' . $episode_id;
                    }
                }
            }
        }

        return $data;
    }

    // 根据ID获取配置的自定义收费配置
    public function getMountedRechargeConfigDetailById($id)
    {
        $data = $this->mountedAppletCustomRechargeConfigModel->getInfoByWhere(['id' => $id], 'id, video_id, platform, sort, pay_amount, recharge_template_id, is_new_version');

        if (!empty($data['is_new_version'])) {
            $rechargeTemplateData = $this->customRechargeTemplateModel->getInfoByWhere(['id' => $data['recharge_template_id']], 'name');
        } else {
            $rechargeTemplateData = $this->rechargeTemplateModel->getInfoByWhere(['id' => $data['recharge_template_id']], 'name');
        }

        // $rechargeTemplateData = $this->rechargeTemplateModel->getInfoByWhere(['id' => $data['recharge_template_id']], 'name');
        $data['recharge_template_name'] = !empty($rechargeTemplateData['name']) ? $rechargeTemplateData['name'] : '';
        return $data;
    }
}